﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BPM.Foundation.PWDB;

namespace BPM.Foundation.PWService
{

    public partial interface IPowerService
    {
        [System.ServiceModel.OperationContract]
        List<PW_Duty> getDutyFromUserDepartment(string pid, string userNo, string departmentNo);

        [System.ServiceModel.OperationContract]
        string getExpansionValue(string pid,string tableName,string businesskey,string itemName);
    }


    public partial class PowerService :IPowerService
    {
        
       
        //[8.1] 得到指定人员在指定部门的任务列表
        public List<PW_Duty> getDutyFromUserDepartment(string pid, string userNo, string departmentNo)
        {
            //[1]
            if(string.IsNullOrEmpty(userNo))
            {
                return null;
            }
            //-

            //[2]
            if(string.IsNullOrEmpty(departmentNo))
            {
                return null;
            }
            //-

            //[3]

            string office = "officeuser";

            List<string> strDutyList = new List<string>();
            strDutyList = PWDB.PW_DepartmentDutyUserRelation.Where(p => p.userNo == userNo && p.departmentNo == departmentNo).Select(p => p.dutyNo).ToList().Distinct().ToList();

            if (!strDutyList.Contains(office))
            {
                strDutyList.Add(office);
            }

            List<PW_Duty> listDuty=new List<PW_Duty>();
            foreach(string dutyNo in strDutyList)
            {
               listDuty.Add ( PWDB.PW_Duty.Single(p=>p.dutyNo==dutyNo));
            }

            return listDuty;

            
        }
        //-

        //[8.2]得到扩展信息值
        public string getExpansionValue(string pid,string tableName, string businesskey, string itemName)
        {
            
            //[1]
            if(string.IsNullOrEmpty(tableName))
            {
                return null;
            }
            //-

            //[2]
            if(string.IsNullOrEmpty(businesskey))
            {
                return null;
            }
            //-

            //[3]
            if(string.IsNullOrEmpty(itemName))
            {
                return null;
            }

            string result=null;
            //-
            switch (tableName)
            {
                case "PW_Department":
                    result = PWDB.PW_DepartmentExpansionInfo
                        .Where(p => p.businessKey == businesskey && p.itemName == itemName)
                        .Select(p=>p.itemValue).First();

                    break;
                case "PW_Duty":
                    result=PWDB.PW_DutyExpansionInfo
                        .Where(p => p.businessKey == businesskey && p.itemName == itemName)
                        .Select(p => p.itemValue).First();
                    break;
                case "PW_User":
                    result=PWDB.PW_UserExpansionInfo
                         .Where(p => p.businessKey == businesskey && p.itemName == itemName)
                        .Select(p => p.itemValue).First();
                    break;
                        
                default:
                    break;
            }

            //[4]
            if(string.IsNullOrEmpty(result))
            {
                return null; 
            }
            return result;
        }

    }
}
